package com.symbio.ishenapi.background.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.symbio.ishenapi.common.bean.QueryModel;
import com.symbio.ishenapi.common.excel.ExcelImport;
import com.symbio.ishenapi.common.kit.ExcelImportUtils;
import io.jboot.db.annotation.Table;
import com.symbio.ishenapi.background.model.base.BaseTmDiseaseBase;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Generated by Jboot.
 */
@Table(tableName = "tm_disease_base", primaryKey = "id")
public class TmDiseaseBase extends BaseTmDiseaseBase<TmDiseaseBase> {

    public static TmDiseaseBase dao = new TmDiseaseBase();

    /**
     * 列表查询
     * @return
     */
    public Map findList(QueryModel queryModel) {
        Map<String,Object> map = new HashMap<>();
        String countSql = "SELECT count(id) FROM tm_disease_base WHERE status = 1 ";
        String sql = "SELECT * FROM tm_disease_base WHERE status = 1 ORDER BY createTime DESC limit "+queryModel.getPage()+","+queryModel.getLimit()+"";
        int count = Db.queryInt(countSql);
        List<Record> list = Db.find(sql);
        map.put("count",count);
        map.put("data",list);
        return map;
    }

    /**
     * wy:医生端
     * 根据条件列表查询
     * @return
     */
    public Map findNameList(QueryModel queryModel,String diseaseName) {
        Map<String,Object> map = new HashMap<>();
        String countSql = "SELECT count(id) FROM tm_disease_base WHERE `status` = '1'";
        String sql = "SELECT * FROM tm_disease_base WHERE status = '1'";
        if (diseaseName != null && !"".equals(diseaseName)){
            countSql += " AND diseaseName LIKE '%"+diseaseName+"%'";
            sql += " AND diseaseName LIKE '%"+diseaseName+"%'";
        }
        sql += " limit "+queryModel.getPage()+","+queryModel.getLimit();
        int count = Db.queryInt(countSql);
        List<Record> list = Db.find(sql);
        map.put("count",count);
        map.put("data",list);
        return map;
    }

    /**
     * 疾病逻辑删
     */
    public int deleteDiseaseBase(TmDiseaseBase tmDiseaseBase) {
        String sql = "UPDATE tm_disease_base SET STATUS = 0 WHERE id = ?";
        return Db.update(sql,tmDiseaseBase.getId());
    }

    /**
     * 初始化疾病数据
     * @param file
     * @return
     */
    public int initData(File file) {
        int count = 0;
        try {
            List<String[]> data = ExcelImport.readExcel(file,2);
            ExecutorService fixedThreadPool = Executors.newFixedThreadPool(20);
            if(data.size()>0){
                for(int i=0;i<data.size();i++){
                    String[] dataRow = data.get(i);
                    fixedThreadPool.execute(new Runnable() {
                        @Override
                        public void run() {
                            TmDiseaseBase tmDiseaseBase = new TmDiseaseBase();
                            tmDiseaseBase.setDiseaseCode(dataRow[0]);
                            tmDiseaseBase.setDiseaseName(dataRow[2]);
                            tmDiseaseBase.setDiseaseStatisticalCodes(dataRow[1]);
                            tmDiseaseBase.setStatus("1");
                            tmDiseaseBase.setCreateTime(new Date());
                            tmDiseaseBase.save();
                        }
                    });
                    count++;
                }
            }
            fixedThreadPool.shutdown();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return count;
    }

    public void importDisease(File file) {
        List<TmDiseaseBase> list = ExcelImportUtils.analysisExcel(file, TmDiseaseBase.class);
        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                TmDiseaseBase diseaseVO = list.get(i);
                diseaseVO.setStatus("1");
                diseaseVO.setCreateTime(new Date());
                diseaseVO.save();
            }
        }
    }
}
